const express = require('express')
const bodyParser = require('body-parser');
const secretKey = 'zhaoying-targetportal'

const app = express()
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const expressJWT = require('express-jwt')
app.use(expressJWT({ secret: secretKey }).unless({ path: [/^\/api/] }))

const cors = require('cors')
app.use(cors())
app.use((req, res,next) => {
    res.err = (message, status =1) => {
        res.send({message, status})
    }
    res.success = (message, status =1) => {
        res.send({message, status})
    }
    next()
})
const userRouter = require('./Router/user')
app.use('/api', userRouter)
app.use((err, req, res,next) => {
    if(err.name === 'UnauthorizedError') {
        return res.send({
            statues: 401,
            message: 'token过期'
        })
    }
    res.send({
        status: 500,
        message: '未知错误'
    })
})
app.listen(3001, () =>  {
    console.log('Server Start');
})